package thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
        new Thread(() -> {
            System.out.println(cyclicBarrier.getNumberWaiting());
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println("hello");
        }).start();
        new Thread(() -> {
            System.out.println(cyclicBarrier.getNumberWaiting());
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println("hello");
        }).start();
        System.out.println(cyclicBarrier.getNumberWaiting());
        cyclicBarrier.await();
        System.out.println("hello");
    }
}
